import {
  createSlice,
  createAsyncThunk
} from '@reduxjs/toolkit'

import {
  getMoviesRequset
} from '../../api/index'

export const getMoviesAction = createAsyncThunk('getMovies', async (data) => {
  const {
    data: {
      list
    }
  } = await getMoviesRequset()
  return list
})

const movieSlice = createSlice({
  name: 'movie',
  initialState: {
    movieList: []
  },
  reducers: {},
  extraReducers: {
    [getMoviesAction.fulfilled](state, {
      payload
    }) {
      state.movieList = payload
    }
  }
})

export default movieSlice.reducer